package com.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.beans.Department;
import com.beans.SalaryManage;
import com.services.DepartmentService;
import com.services.SalaryService;

/**
 * Servlet implementation class SalaryManageServlet
 */
@WebServlet("/salary")
public class SalaryManageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
      private SalaryService sa=new SalaryService();
      private DepartmentService dp=new DepartmentService();
    public SalaryManageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("method");
		if (method == null) {
			method = "selectAll";
		}
		switch (method) {
		case "selectAll":
			List<SalaryManage> list = sa.selectAll();
			request.setAttribute("list", list);
			
			request.getRequestDispatcher("salary.jsp").forward(request, response);
			break;
		case "delete":
			String idStr = request.getParameter("id");
			int id = Integer.parseInt(idStr);
			sa.delete(id);
			response.sendRedirect("/salary_manage/salary");
			break;
		case "findById":
			idStr = request.getParameter("id");
			id = Integer.parseInt(idStr);
			request.setAttribute("salary", sa.findById(id));
			request.getRequestDispatcher("updateSalary.jsp").forward(request, response);
			break;
		case "add":
			List<Department> deplist=dp.selectAll();
			request.setAttribute("list", deplist);
			request.getRequestDispatcher("addSalary.jsp").forward(request, response);
			break;

		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String method = request.getParameter("method");
		
		
		String post=request.getParameter("post");
		
		String baseWageStr=request.getParameter("baseWage");
		double baseWage=Double.parseDouble(baseWageStr);
		
		
		String allowanceStr=request.getParameter("allowance");
		double allowance=Double.parseDouble(allowanceStr);
		
		String lunchStr=request.getParameter("lunch");
		double lunch=Double.parseDouble(lunchStr);
		
		String otherPayStr=request.getParameter("otherPay");
		double otherPay=Double.parseDouble(otherPayStr);
		
		String overtimeWageStr=request.getParameter("overtimeWage");
		double overtimeWage=Double.parseDouble(overtimeWageStr);
		switch (method) {
		case "add":
			String departmentStr = request.getParameter("departmentSno");
			System.out.println(departmentStr);
			int departmentSno = Integer.parseInt(departmentStr);
			sa.add(departmentSno,post,baseWage, allowance, lunch,otherPay,overtimeWage);	
			break;
		case "update":
			String idStr = request.getParameter("id");
			int id = Integer.parseInt(idStr);
			sa.update(id,post,baseWage, allowance, lunch,otherPay,overtimeWage);
			
			break;
		}
		response.sendRedirect("/salary_manage/salary");
	}
}
